define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Proconsult, GoPage, Auth, GoCatTypeAuth, GoDialog,$window, $timeout, $ionicSideMenuDelegate) {

		$scope.cat_type = 18;
		if($scope.input == undefined) {
			$scope.input = new Object();
		}
		$scope.input.key = "";
		$scope.input.nPage = 1;
		var store_cat = "";
		//加载数据
		$scope.init_data = function() {
			Proconsult.getlist($scope.cat_type, 1, $scope.input.key, store_cat, 10, 1, function(data) {
				$scope.list = data;
				if(data!=null){
					$scope.totalResult = data.page.count; //共有多少条数据
				}else{
					$scope.totalResult = 0;
				}
			});
		};
		$scope.init_data();

		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proconsult.getlist($scope.cat_type, 1, $scope.input.key, store_cat, 10, 1, function(data) {
					if(data != null) {
						$scope.list = data;
					} else {
						$scope.list = new Object();
					}
					$scope.input.nPage = 1;
				},
				function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
		};

		$scope.goods_info = function(goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" });
		};

		//加载更多
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Proconsult.getlist($scope.cat_type, 1, $scope.input.key, store_cat, 10, $scope.input.nPage + 1, function(data) {
					if(data != null) {
						if($scope.list != undefined) {
							$scope.list.data = $scope.list.data.concat(data.data);
							$scope.input.nPage += 1;
						} else {
							$scope.list = data;
						}
					}
				},
				function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);

				});
		};
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//查询
		$scope.doSearch = function() {
			store_cat = '';
			if($scope.input.cat3 != null) {
				store_cat = $scope.input.cat1.id + "," + $scope.input.cat2.id + "," + $scope.input.cat3.id;
			} else if($scope.input.cat2 != null) {
				store_cat = $scope.input.cat1.id + "," + $scope.input.cat2.id;
			} else if($scope.input.cat1 != null) {
				store_cat = $scope.input.cat1.id;
			}
			Proconsult.getlist($scope.cat_type, 1, $scope.input.key, store_cat, 10, 1, function(data) {
				$scope.list = data;
				$scope.totalResult = data.page.count;
			});
			$ionicSideMenuDelegate.toggleRight();
		};
		//取消
		$scope.doCancel = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		//跳转设置画面
		$scope.onshow = function(value, video_type) {
			if(value == 2 || value == 3 || value == 4)
				GoPage.go_states("proconsultservice", { "video_type": video_type });
			if(value == 5)
				GoPage.go_states("proconsultproblem");
			if(value == 6)
				GoPage.go_states("salesoutlets");
		};

		/***分类三级联动***/
		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听
		$scope.listen_cat1 = function (data) {
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			$scope.cat3_items = '';
		};
		$scope.listen_cat2 = function (data) {
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		$scope.doSelectCat1 = function () {
			//id是值 name是显示
			GoDialog.showSelect("大分类", $scope.cat1_items, $scope.input.cat1, function (data) {
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function () {
			//id是值 name是显示
			GoDialog.showSelect("中分类", $scope.cat2_items, $scope.input.cat2, function (data) {
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function () {
			//id是值 name是显示
			GoDialog.showSelect("小分类", $scope.cat3_items, $scope.input.cat3, function (data) {
				$scope.input.cat3 = data;
			});
		}
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);

		
		//打开txcall go_txcall  onshow_outLink
		$scope.onshow_txcall = function(store_id) {
			GoPage.go_txcall("",store_id);
		};

	}

	controller.$inject = ['$scope', 'Proconsult', 'GoPage', 'Auth', 'GoCatTypeAuth','GoDialog', '$window', '$timeout', '$ionicSideMenuDelegate'];

	return app.registerController("proconsultctrl",controller);
});